﻿using System;
using System.Windows.Forms;
using Infrastructure;
using Presentation;

namespace HuffmanCoding
{
    public partial class HuffmanCodingForm : Form, IView
    {
        public HuffmanCodingForm()
        {
            InitializeComponent();
        }


        public string Log
        {
            get
            {
                return TextBoxLog.Text;
            }
            set
            {
                TextBoxLog.Text = value;
            }
        }

        public string CharToCode
        {
            get
            {
                return TextBoxCharToCode.Text;
            }
            set
            {
                TextBoxCharToCode.Text = value;
            }
        }

        public string CodeToChar
        {
            get
            {
                return TextBoxCodeToChar.Text;
            }
            set
            {
                TextBoxCodeToChar.Text = value;
            }
        }

        public string CharacterString
        {
            get
            {
                return TextBoxString.Text;
            }
            set
            {
                TextBoxString.Text = value;
            }
        }
        public string HuffmanCode
        {
            get
            {
                return TextBoxCode.Text;
            }
            set
            {
                TextBoxCode.Text = value;
            }
        }
        public string Status
        {
            get
            {
                return ToolLabelError.Text;
            }
            set
            {
                ToolLabelError.Text = value;
            }
        }

        public event Action OnEncode;
        public event Action OnDecode;
        public event Action OnStringChanged;
        public event Action OnCodeChanged;
        public event Action OnBuildTree;

        private void CodeChanged(object sender, EventArgs e)
        {
            OnCodeChanged();
            if (String.Equals(TextBoxCode.Text, ""))
            {
                ButtonDecode.Enabled = false;
            }
            else
            {
                ButtonDecode.Enabled = true;
            }
        }

        private void ButtonEncode_Click(object sender, EventArgs e)
        {
            OnEncode();
        }

        private void ButtonDecode_Click(object sender, EventArgs e)
        {
            OnDecode();
        }

        private void RadioHtml_CheckedChanged(object sender, EventArgs e)
        {
            Program.SetLogger(new HtmlLogger());
        }

        private void RadioTxt_CheckedChanged(object sender, EventArgs e)
        {
            Program.SetLogger(new TxtLogger());
        }

        private void StringChanged(object sender, EventArgs e)
        {
            OnStringChanged();
            if (String.Equals(TextBoxString.Text, ""))
            {
                ButtonBuildTree.Enabled = false;
            }
            else
            {
                ButtonBuildTree.Enabled = true;
            }
        }

        private void ButtonBuildTree_Click(object sender, EventArgs e)
        {
            OnBuildTree();
            ButtonDecode.Enabled = true;
            ButtonEncode.Enabled = true;
            TextBoxCode.Enabled = true;
        }

        private void TextBoxCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '0' && e.KeyChar != '1' && e.KeyChar != '\b') e.KeyChar = '\0';
        }

    }
}
